package com.xyy.saas.payment.callback.core.job;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.xyy.elasticjob.client.annotation.ElasticJobConf;
import com.xyy.saas.payment.payment.core.service.RechargeFallBackService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @author wangzeqi
 * @see com.xyy.saas.payment.util.mq.CallbackRechargeHandleConsumer
 */
@Slf4j
@ElasticJobConf(name = "com.xyy.saas.payment.callback.core.job.RechargeFallBackJob", cron = "0 0 22 * * ?")
@RequiredArgsConstructor
public class RechargeFallBackJob implements SimpleJob {

    private final RechargeFallBackService rechargeFallBackService;

    @Override
    public void execute(ShardingContext shardingContext) {
        long startTime = System.currentTimeMillis();
        log.info("RechargeFallBackJob execute start.");

        LocalDateTime beginTime = LocalDate.now().minusDays(1).atStartOfDay();
        LocalDateTime endTime = LocalDateTime.now();

        try {
            rechargeFallBackService.handlePayRecharge(beginTime, endTime);
        } catch (Exception e) {
            log.error("RechargeFallBackJob execute handlePayRecharge error.", e);
        }

        try {
            rechargeFallBackService.handleRefundRecharge(beginTime, endTime);
        } catch (Exception e) {
            log.error("RechargeFallBackJob execute handleRefundRecharge error.", e);
        }

        log.info("RechargeFallBackJob execute end. costTime:{}ms", System.currentTimeMillis() - startTime);
    }

}
